package com.ia.uniweb.userfacing;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ia.uniweb.cass.CassandraProxy;
import com.ia.uniweb.model.Result;
import com.ia.uniweb.service.ResultService;
import com.ia.uniweb.service.ResultServiceImpl;

public class ResultsLet extends HttpServlet {

	private ResultService resultService;

	private CassandraProxy cassandra;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		// Initialise cassandra
		cassandra = CassandraProxy.initialise();

		resultService = new ResultServiceImpl(cassandra.getConnectedKeyspace());

		System.out.println("Initialised servlet and cassandra");

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String requestType = request.getParameter("requestType");

		if (requestType != null && !requestType.isEmpty()) {

			if (requestType.equals("search")) {
				searchResult(request, response);
			} else if (requestType.equals("create")) {
				createResult(request, response);
			} else if (requestType.equals("update")) {
				updateResult(request, response);
			} else {
				// NoOp
			}

		} else {

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/views/display/unknown.jsp");
			if (dispatcher != null) {
				dispatcher.forward(request, response);
			}

		}

	}

	private void updateResult(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	private void createResult(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String resultKey = (String) request.getParameter("upload-result-key");
		if (resultKey != null && !resultKey.isEmpty()) {

			int numResults = (int) Integer.valueOf((String) request
					.getParameter("upload-result-number"));

			Result[] results = new Result[numResults];
			
			for (int i = 0; i < numResults; i++) {

				Result result = new Result();
				result.setRegistrationNumber(resultKey);
				result.setSubjectCode((String)request.getParameter("upload-subjectCode" + i));
				result.setSubject((String)request.getParameter("upload-subject" + i));
				result.setGrade((String)request.getParameter("upload-grade" + i));
				result.setResult((String)request.getParameter("upload-result" + i));
				result.setInternalMarks(Integer.valueOf(((String)request.getParameter("upload-iaMark" + i))));
				result.setPracticalMarks(Integer.valueOf(((String)request.getParameter("upload-pracMark" + i))));
				result.setTheoryMarks(Integer.valueOf(((String)request.getParameter("upload-theoryMark" + i))));
				results[i] = result;
			}

			boolean uploadStatus = resultService.uploadResultsForStudent(resultKey, Arrays.asList(results));
			RequestDispatcher dispatcher;
			if(uploadStatus)
			{
				 dispatcher = request
						.getRequestDispatcher("/views/display/success.jsp");
				if (dispatcher != null) {
					dispatcher.forward(request, response);
				}
				
			} 
				else 
			{
					dispatcher = request
							.getRequestDispatcher("/views/display/error.jsp");
					if (dispatcher != null) {
						dispatcher.forward(request, response);
					}
			}
			
		}

	}

	private void searchResult(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// If there is a search parameter in the request,
		// lookup and show profile accordingly

		String resultQuery = (String) request.getParameter("search-result");
		List<Result> results = null;
		if (resultQuery != null) {

			// Query cassandra
			results = resultService.getResultsForStudent(resultQuery);
			request.setAttribute("result-result", results);
		}

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/views/display/results.jsp");
		if (dispatcher != null) {
			dispatcher.forward(request, response);
		}

	}

}
